package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

func main() {
	// 1.GET请求示例
	// resp, err := http.Get("https://www.baidu.com/")
	// if err != nil {
	// 	fmt.Println("get failed, err:", err)
    //     return
	// }
	// defer resp.Body.Close()
	// body, err := ioutil.ReadAll(resp.Body)
	// if err != nil {
    //     fmt.Println("read from resp.Body failed,err:", err)
    //     return
    // } 
	// fmt.Print(string(body))

	// 2.带参数的GET请求示例
	apiUrl := "http://127.0.0.1:9090/get"
	data := url.Values{}
	data.Set("name", "枯藤") 
	data.Set("age", "18")
	u, err := url.ParseRequestURI(apiUrl)
	if err != nil {
        fmt.Printf("parse url requestUrl failed,err:%v\n", err)
    }
	u.RawQuery = data.Encode()
	fmt.Println(u.String())
	resp, err := http.Get(u.String())
	if err != nil {
		fmt.Println("post failed, err:%v", err)
		return 
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body) 
	if err != nil {
		fmt.Println("get resp failed,err:%v\n", err)
        return
	}
	fmt.Println(string(b))
}